<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="请输入搜索的文案标题" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePageInit">搜索</el-button>
      <el-button type="success" size="mini" icon="el-icon-share" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table :data="responseData.tableList"
              border style="width: 100%;"
              v-loading="pageData.loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
              @selection-change="handleTableSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed="left" prop="onlineClassId" label="数据ID" width="320" align="center"/>
      <el-table-column prop="onlineClassName" label="课程名称" width="240" align="center">
        <template slot-scope="scope">
          <span class="link-type" @click="handleOpenEditPage(scope)">{{ scope.row.onlineClassName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassImage" label="展示图" width="120" align="center">
        <template slot-scope="scope">
              <span v-if="scope.row.onlineClassImage != null && scope.row.onlineClassImage !== ''">
                <el-button size="small" width="120" type="success" @click="handleOpenCheckPanel(scope.row.onlineClassImage)">预览</el-button>
              </span>
          <span v-else >无</span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassShareImage" label="分享图" width="120" align="center">
        <template slot-scope="scope">
              <span v-if="scope.row.onlineClassShareImage != null && scope.row.onlineClassShareImage !== ''">
                <el-button size="small" width="120" type="success" @click="handleOpenCheckPanel(scope.row.onlineClassShareImage)">预览</el-button>
              </span>
          <span v-else >无</span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassHomeImage" label="首页图" width="120" align="center">
        <template slot-scope="scope">
              <span v-if="scope.row.onlineClassHomeImage != null && scope.row.onlineClassHomeImage !== ''">
                <el-button size="small" width="120" type="success" @click="handleOpenCheckPanel(scope.row.onlineClassHomeImage)">预览</el-button>
              </span>
          <span v-else >无</span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassIntro" label="课程简介" width="320" align="center"/>
      <el-table-column prop="onlineClassStatus" label="审核状态" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.onlineClassStatus === 0"><el-tag type="warning">未发布</el-tag></span>
          <span v-if="scope.row.onlineClassStatus === 1"><el-tag type="success">发布</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassShowIndex" label="展示顺序" width="100" align="center"/>
      <el-table-column prop="onlineClassShareTitle" label="分享标题" width="320" align="center"/>
      <el-table-column prop="onlineClassHtml" label="分享介绍页" width="240" align="center">
        <template slot-scope="scope">
          <span class="link-type"><a target="_blank" :href="scope.row.onlineClassHtml">{{ scope.row.onlineClassHtml }}</a></span>
        </template>
      </el-table-column>
      <el-table-column prop="onlineClassCreateTime" label="创建时间" width="320" align="center"/>
      <el-table-column fixed="right" label="操作" width="320" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleOpenEditPage(scope)">编辑</el-button>
          <el-button type="success" size="small" @click="handleOpenCopyPanel(scope)">一键复制</el-button>
          <el-button v-if="scope.row.onlineClassStatus === 0" size="mini" type="success" @click="handleEditDataStatus(scope, 1)">发布</el-button>
          <el-button v-if="scope.row.onlineClassStatus === 1" size="mini" type="info" @click="handleEditDataStatus(scope, 0)">下架</el-button>
          <el-button type="danger" size="mini" @click="handleDeleteData(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="form.pageNum"
      :page-size="form.pageSize"
      :total="form.total"
      :page-sizes="[12, 50, 100, 200]"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange">
    </el-pagination>
    <!-- 图片预览隐藏域 -->
    <el-dialog title="图片预览" :visible.sync="pageData.checkPanelStatus" size="tiny" width="480px"
               :before-close="handleCloseCheckPanel">
      <el-card :body-style="{ padding: '0px' }">
        <div>
          <div>
            <img :src="pageData.checkPanelInfo" width="450px" height="450px">
          </div>
        </div>
      </el-card>
    </el-dialog>
    <!-- 一键复制课程隐藏域 -->
    <el-dialog title="课程复制" :visible.sync="pageData.copyPanelStatus" size="tiny" width="480px"
               :before-close="handleCloseCopyPanel">
      <el-card :body-style="{ padding: '0px' }">
        <el-select v-model="form.copyClass.onlineClassTargetId"
                   filterable remote reserve-keyword
                   placeholder="请选择推荐文章"
                   :remote-method="handleRemoteData">
          <el-option
            v-for="item in responseData.remoteSearchList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.type }}</span>
          </el-option>
        </el-select>
      </el-card>
      <el-button type="primary" size="small" @click="handleSaveButtonClick">确定复制</el-button>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsClassList,batchReleaseCmsClassData,loadCmsClassDataList,saveCmsClassCopyData } from "@/api/study";
  export default {
    name: "class",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchStatusList: [
            {label: "发布状态", value: -1},
            {label: "发布", value: 1},
            {label: "未发布", value: 0}
          ],
          loading: false,
          checkPanelStatus: false,
          checkPanelInfo: "",
          copyPanelStatus: false,
        },// 页面配置数据
        form:{
          searchTitle: "",
          searchStatus: -1,
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: [],// 多选的内容
          copyClass: {
            onlineClassId: "",
            onlineClassTargetId: ""
          }
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: [],
          remoteSearchList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsClassList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsClassList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsClassList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].onlineClassId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleBatchReleaseData(){
        // 判断该用户是否有更改发布状态的权限
        if (PermissionsToDetectFormUser(getPermission(), "600005")){
          this.$confirm('此操作将会把所选数据变为发布状态, 请核对后继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 拼接请求参数参数
            let param = {
              onlineClassId: this.form.selection.toString(),
              onlineClassStatus: 1
            };
            this.batchReleaseCmsClassData(param);
            // 接口访问
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布按钮点击事件
      handleOpenCheckPanel(image){
        // 初始化表单
        this.pageData.checkPanelInfo = image;
        // 打开面板
        this.pageData.checkPanelStatus = true;
      },// 打开预览页面
      handleCloseCheckPanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化表单
          this.pageData.checkPanelInfo = "";
          // 打开面板
          this.pageData.checkPanelStatus = false;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭图像预览页面
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "600003")){
          this.$jump.push({path: '/study/classEdit',query: {onlineClassId: scope.row.onlineClassId}});
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改页面
      handleOpenCopyPanel(scope){
        if (scope.row.onlineClassStatus == 1){
          this.$message.warning("当前课程已发布,如需调整课程，请联系管理员");
          return;
        }
        // 初始化表单
        this.form.copyClass.onlineClassId = scope.row.onlineClassId;
        this.form.copyClass.targetId = "";
        // 打开面板
        this.pageData.copyPanelStatus = true;
      },// 打开课程复制面板
      handleCloseCopyPanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化表单
          this.form.copyClass.onlineClassId = "";
          this.form.copyClass.onlineClassTargetId = "";
          // 打开面板
          this.pageData.copyPanelStatus = false;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭复制面板
      handleEditDataStatus(scope, status){
        if (PermissionsToDetectFormUser(getPermission(), "600005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              onlineClassId: scope.row.onlineClassId,
              onlineClassStatus: status
            };
            this.batchReleaseCmsClassData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 修改数据发布状态
      handleDeleteData(scope){
        this.$message.info("该功能暂未开放，如有需要请联系管理员");
      },// 删除按钮点击事件
      handleRemoteData(query){
        if (query !== "") {
          let param = {
            searchTitle: query,
          };
          this.loadCmsClassDataList(param);
        } else {
          this.ProfessorClassPushPanelRemoteDataList = [];
        }
      },// 课程推送面板搜索内容
      handleSaveButtonClick(){
        this.$confirm('当前操作会将所选课程数据完全复制到当前课程内, 请核对后继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.saveCmsClassCopyData();
        }).catch(close => {
          console.log(close);
          this.$message.info("已取消操作");
        });
      },// 复制保存按钮点击事件
      // 接口
      loadCmsClassList(){
        loadCmsClassList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取课程列表
      batchReleaseCmsClassData(param){
        batchReleaseCmsClassData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.onlineClassStatus == 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量修改数据发布状态
      loadCmsClassDataList(param){
        loadCmsClassDataList(param).then(response => {
          if (response.code === '200'){
            this.responseData.remoteSearchList = response.data
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 获取远程可用课程列表
      saveCmsClassCopyData(){
        saveCmsClassCopyData(this.form.copyClass).then(response =>{
          if (response.code === "200"){
            this.$message.success("一键复制成功");
            // 初始化表单
            this.form.copyClass.onlineClassId = "";
            this.form.copyClass.onlineClassTargetId = "";
            // 打开面板
            this.pageData.copyPanelStatus = false;
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
        })
      },// 保存复制数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
